import 'package:flutter/material.dart';
import 'package:library_base/constant/constant.dart';
import 'package:library_base/entities/buyer_entity.dart';
import 'package:library_base/ext/list_ext.dart';
import 'package:library_base/global/multi_language.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:library_base/net/apis.dart';
import 'package:library_base/network/http_config.dart';
import 'package:library_base/res/edge.dart';
import 'package:library_base/res/gaps.dart';
import 'package:library_base/res/styles.dart';
import 'package:library_base/route/routers.dart';
import 'package:library_base/utils/theme_util.dart';
import 'package:library_base/utils/toast_util.dart';
import 'package:library_base/utils/widget_generate_util.dart';
import 'package:library_base/widget/button/gradient_button.dart';
import 'package:library_base/widget/image/local_image.dart';
import 'package:library_base/widget/multi_state_widget/multi_state_widget.dart';
import 'package:library_base/base/base_list_state.dart';
import 'package:module_administrator/page/tasks/task_list_controller.dart';
import 'package:library_base/entities/tasks_entity.dart';

class TasksListPage extends StatefulWidget {
  const TasksListPage({Key? key}) : super(key: key);

  @override
  _TasksListPageState createState() => _TasksListPageState();
}

class _TasksListPageState
    extends BaseListState<TasksEntity, TasksEntity, TasksListPage, TasksListController> {
  @override
  TasksListController registerController() => Get.find<TasksListController>();

  @override
  void onCreate() {
    super.onCreate();
    setTitle(MultiLanguage.purchaseOrderTask.tr);
  }

  @override
  void controllerOnCreate() {
    useRefresh = true;
    isCanRefresh = false;
    isCanLoad = false;
    enableControlFinishLoad = false;
    enableControlFinishRefresh = true;
    baseGetxController?.easyRefreshController = controller;
  }

  @override
  Widget? getMainWidget(BuildContext context) {
    return MultiStateWidget(
      emptyWidget: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            LocalImage('admin_empty', package: Constant.packAdmin(), width: 0.6.sw),
            Text(MultiLanguage.noSuchProduct.tr, style: TextStyles.textSecondary28),
            SizedBox(height: 80.h),
            Row(mainAxisAlignment: MainAxisAlignment.center, children: [
              GradientButton(
                ThemeUtil.getTheme(),
                width: 0.4.sw,
                height: 80.w,
                text: MultiLanguage.reload.tr,
                onPressed: baseGetxController!.tryReLoad,
              ),
            ])
          ]),
      controller: baseGetxController!.multiControllerOut,
      child: null,
      buildChild: (){
        return Column(
          children: [
            Expanded(
              child: getListView(context, baseGetxController),
            ),
            Container(
                height: 166.w,
                decoration: const BoxDecoration(color: Colors.white, boxShadow: [
                  BoxShadow(
                      offset: Offset(0.0, 5.0), //阴影xy轴偏移量
                      blurRadius: 15.0, //阴影模糊程度
                      spreadRadius: -11 //阴影扩散程度
                  )
                ]),
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Expanded(
                      child: Stack(
                        children: [
                          Positioned(
                            left: 24.w,
                            top: 43.w,
                            child: GetBuilder<TasksListController>(
                              builder: (logic) {
                                return GestureDetector(
                                  onTap: baseGetxController!.selectedAll,
                                  child: Container(
                                    color: Colors.transparent,
                                    child: Row(
                                      mainAxisSize: MainAxisSize.min,
                                      children: [
                                        LocalImage(
                                            logic.isSelectAll.value
                                                ? 'admin_checked'
                                                : 'admin_no_checked',
                                            package: Constant.packAdmin(),
                                            width: 34.r,
                                            height: 34.r),
                                        gapW10,
                                        Text(
                                            MultiLanguage.selectAll2OrdersSelected
                                                .trArgs(["${logic.listDataSelected.length}"]),
                                            style: TextStyles.textNormal28)
                                      ],
                                    ),
                                  ),
                                );
                              },
                            ),
                          ),
                          // Positioned(
                          //   right: 24.w,
                          //   top: 21.w,
                          //   child: GetBuilder<TasksListController>(
                          //     builder: (logic) {
                          //       return GradientButton(
                          //         ThemeUtil.getTheme(),
                          //         height: 77.w,
                          //         padding: EdgeInsets.symmetric(horizontal: 30.w),
                          //         text: Messages.changeBuyer.tr,
                          //         onPressed: baseGetxController!.listDataSelected.value.isEmpty
                          //             ? null
                          //             : () {
                          //                 List<String> purchaseIds = [];
                          //                 for (var element
                          //                     in baseGetxController!.listDataSelected.value) {
                          //                   if (element.isSelect) {
                          //                     purchaseIds.add(element.purchaseId);
                          //                   }
                          //                 }
                          //                 if (purchaseIds.isEmpty) {
                          //                   ToastUtil.waring('请先选择采购单任务');
                          //                   return;
                          //                 }
                          //                 baseGetxController?.requestUniversal<BuyerEntity>(
                          //                     Apis.BUYER,
                          //                     queryParameters: {'postCode': 'purchaser'},
                          //                     method: FBResultFul.GET,
                          //                     resultSuccessListCall: (list) async {
                          //                   Get.toNamed(Routers.chooseBuyerPage, arguments: {
                          //                     'Buyer': list,
                          //                     'purchaseIds': purchaseIds,
                          //                     'callback': baseGetxController!.tryReLoa1d
                          //                   });
                          //                 });
                          //               },
                          //       );
                          //     },
                          //   ),
                          // ),
                        ],
                      ),
                    ),
                    GetBuilder<TasksListController>(
                      builder: (logic) {
                        return GradientButton(
                          ThemeUtil.getTheme(),
                          height: 77.w,
                          margin: EdgeInsets.only(top: 21.w, right: 24.w),
                          padding: EdgeInsets.symmetric(horizontal: 30.w),
                          text: MultiLanguage.changeBuyer.tr,
                          onPressed: baseGetxController!.listDataSelected.value.isEmpty
                              ? null
                              : () {
                            List<String> purchaseIds = [];
                            for (var element in baseGetxController!.listDataSelected.value) {
                              if (element.isSelect) {
                                purchaseIds.add(element.purchaseId);
                              }
                            }
                            if (purchaseIds.isEmpty) {
                              ToastUtil.waring('请先选择采购单任务');
                              return;
                            }
                            baseGetxController?.requestUniversal<BuyerEntity>(Apis.BUYER,
                                queryParameters: {'postCode': 'purchaser'},
                                method: FBResultFul.GET, resultSuccessListCall: (list) async {
                                  Get.toNamed(Routers.chooseBuyerPage, arguments: {
                                    'Buyer': list,
                                    'purchaseIds': purchaseIds,
                                    'title':MultiLanguage.buyer.tr,
                                    'callback': baseGetxController!.tryReLoa1d
                                  });
                                });
                          },
                        );
                      },
                    ),
                  ],
                )),
          ],
        );
      },
    );
  }

  ///显示选中的商品列表
  void onShowSelectedGoods() {
    if (baseGetxController!.listDataSelected.isEmpty) {
      ToastUtil.normal(MultiLanguage.pleaseSelectTheItemBeforeProceeding.tr);
      return;
    }
  }

  @override
  List<Widget> buildListItem(BuildContext context) {
    return baseGetxController!.listData.value.buildItem((index, element) {
      return GetBuilder<TasksListController>(
        builder: (logic) {
          return GenerateWidgetUtil.buildTaskItem(
            element,
            onTap: baseGetxController!.onItemChildClick,
            onItemClick: baseGetxController!.onItemClick,
          );
        },
      );
    }, dividingLine: () {
      return Gaps.vwGap18;
    });
  }
}
